package com.ygqh.baby.controller.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.PromotionStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgTuan;
import com.ygqh.baby.service.YgTuanService;
import com.ygqh.baby.utils.JsonUtils;

@Controller
@RequestMapping("/admin/tuan")
public class TuanController extends BaseController<Object, java.lang.Long> {
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	private YgTuanService YgTuanService;

	@RequestMapping("searchProductTuan")
	@ResponseBody
	public Map<String, Object> searchProductTuan(String q, QueryInfo queryInfo, PromotionStatus pstatus, Date startDate, Date endDate) throws ParseException {

		ResultSet<YgTuan> resultSet = YgTuanService.search(queryInfo, q, pstatus, startDate, endDate);

		return resultSet.toDatatables(null);
	}

	@RequestMapping("addProductTuan")
	@ResponseBody
	public Message addProductTuan(String tuan,String startDate,String endDate) throws ParseException {
		YgTuan ygTuan = JsonUtils.toObject(tuan, YgTuan.class);
		ygTuan.setStartDate(df.parse(startDate));
		ygTuan.setEndDate(df.parse(endDate));
		ygTuan.setCreateBy(getSession().getTrueName());
		ygTuan.setCreateTime(new Date());
		return YgTuanService.saveOrUpdate(ygTuan)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;

	}

	@RequestMapping("updateTuan")
	@ResponseBody
	public Message updateTuan(String tuan,String startDate,String endDate) throws ParseException {
		YgTuan ygTuan = JsonUtils.toObject(tuan, YgTuan.class);
		ygTuan.setStartDate(df.parse(startDate));
		ygTuan.setEndDate(df.parse(endDate));
		ygTuan.setUpdateBy(getSession().getTrueName());
		ygTuan.setUpdateTime(new Date());
		return YgTuanService.saveOrUpdate(ygTuan)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;
	}

	@RequestMapping("getTuan")
	@ResponseBody
	public YgTuan getTuan(Long id) {
		return YgTuanService.findById(id);
	}


}
